package com.xh.filepicker.sdk;

import android.app.Activity;
import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.xh.filepicker.sdk.models.Config;
import com.xh.filepicker.sdk.ui.FilePickerActivity;

import java.lang.ref.WeakReference;

/**
 * Created by zhuangshaobo on 2021/6/15.
 */
/* This is the configuration class for File Picker */
public final class XHFilePicker {

    public static final String RESULT_DATA = "result_data";

    private final WeakReference<Activity> mActivity;
    private final WeakReference<Fragment> mContext;

    private XHFilePicker(Activity activity) {
        this(activity, null);
    }

    private XHFilePicker(Fragment fragment) {
        this(fragment.getActivity(), fragment);
    }

    public XHFilePicker(Activity activity, Fragment fragment) {
        this.mActivity = new WeakReference<>(activity);
        this.mContext = new WeakReference<>(fragment);
    }

    /**
     * Start UnicornFilePicker from an activity
     *
     * @param activity Activity instance
     * @return UnicornFilePicker instance
     */
    public static XHFilePicker from(Activity activity) {
        return new XHFilePicker(activity);
    }

    /**
     * Start UnicornFilePicker from a fragment
     *
     * @param fragment Fragment instance
     * @return UnicornFilePicker instance
     */
    public static XHFilePicker from(Fragment fragment) {
        return new XHFilePicker(fragment);
    }

    /**
     * Start FilePicker activity and wait for result
     *
     * @param requestCode Integer identity for Activity or Fragment request
     */
    public void forResult(int requestCode) {
        Config.getInstance().setReqCode(requestCode);

        Activity activity = getActivity();
        if (activity == null) {
            return;
        }

        Intent intent = new Intent(activity, FilePickerActivity.class);

        Fragment fragment = getFragment();
        if (fragment == null) {
            activity.startActivityForResult(intent, requestCode);
        } else {
            fragment.startActivityForResult(intent, requestCode);
        }
    }

    public ConfigBuilder addConfigBuilder() {
        return new ConfigBuilder(this);
    }


    @Nullable
    Activity getActivity() {
        return mActivity.get();
    }

    @Nullable
    Fragment getFragment() {
        return mContext.get();
    }

}
